package com.fz.security.handler;

import com.fz.common.ResponseEntity;
import com.fz.common.ResultCode;
import com.fz.utils.JacksonUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *  用户登录后，但是无权限访问资源产生的异常
 */
@Component
public class JwtAccessDeniedHandler implements AccessDeniedHandler {

   @Override
   public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException {

      response.setCharacterEncoding("UTF-8");
      response.setContentType("text/html;charset=utf-8");
      String result= JacksonUtils.obj2String(ResponseEntity.build(ResultCode.NO_PERMISSION));
      response.getWriter().print(result);
   }
}
